package org.jeecg.modules.jxc.homepage.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.modules.jxc.baseData.entity.JxcGoodsBo;
import org.jeecg.modules.jxc.homepage.entity.JxcGoods;

/**
 * @Description: jxc_goods
 * @Author: jeecg-boot
 * @Date:   2021-12-20
 * @Version: V1.0
 */
public interface JxcGoodsMapper extends BaseMapper<JxcGoods> {

    List<JxcGoods> findListByTypeIdAndCode(@Param("goodsTypeId") Integer goodsTypeId,@Param("code") Integer code);

    List<JxcGoods> findListByTypeIdAndGoodsName(@Param("goodsTypeId")Integer goodsTypeId,@Param("goodsNameOrCode") String goodsNameOrCode);


    /**
     * @description: 查询有库存商品-jcj
     * @param key
     * @param pageNo
     * @param pageSize
     * @return java.util.List<org.jeecg.modules.jxc.baseData.entity.JxcGoodsBo>
     * @exception
     */
    @Select("SELECT jg2.* FROM (SELECT * FROM jxc_goods WHERE goods_code LIKE CONCAT('%',#{key},'%') OR goods_name LIKE CONCAT('%',#{key},'%')) jg1 JOIN (SELECT * FROM jxc_goods WHERE inventory_quantity > 0 LIMIT #{pageNo},#{pageSize}) jg2 ON jg1.id = jg2.id")
    List<JxcGoodsBo> selectGoodsList(@Param("key") String key, @Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize);

    /**
     * @description: 查询无库存商品-jcj
     * @param key
     * @param pageNo
     * @param pageSize
     * @return java.util.List<org.jeecg.modules.jxc.baseData.entity.JxcGoodsBo>
     * @exception
     */
    @Select("SELECT jg2.* FROM (SELECT * FROM jxc_goods WHERE goods_code LIKE CONCAT('%',#{key},'%') OR goods_name LIKE CONCAT('%',#{key},'%')) jg1 JOIN (SELECT * FROM jxc_goods WHERE inventory_quantity = 0 LIMIT #{pageNo},#{pageSize}) jg2 ON jg1.id = jg2.id")
    List<JxcGoodsBo> selectNullGoodsList(@Param("key") String key,@Param("pageNo") Integer pageNo,@Param("pageSize") Integer pageSize);

    /**
     * @description: 删除商品库存-jcj
     * @param id
     * @return void
     * @exception
     */
    @Update("UPDATE jxc_goods SET inventory_quantity = 0 WHERE id = #{id}")
    void updateGoodsNumToNull(@Param("id") Integer id);

    /**
     * @description: 查询无库存商品数量
     * @param key
     * @return java.lang.Integer
     * @exception
     */
    @Select("SELECT COUNT(*) FROM (SELECT * FROM jxc_goods WHERE goods_code LIKE CONCAT('%',#{key},'%') OR goods_name LIKE CONCAT('%',#{key},'%')) jg1 JOIN (SELECT * FROM jxc_goods WHERE inventory_quantity = 0) jg2 ON jg1.id = jg2.id")
    Integer selectNullGoodsCount(@Param("key") String key);

    /**
     * @description: 查询有库存商品数量
     * @param key
     * @return java.lang.Integer
     * @exception
     */
    @Select("SELECT COUNT(*) FROM (SELECT * FROM jxc_goods WHERE goods_code LIKE CONCAT('%',#{key},'%') OR goods_name LIKE CONCAT('%',#{key},'%')) jg1 JOIN (SELECT * FROM jxc_goods WHERE inventory_quantity > 0) jg2 ON jg1.id = jg2.id")
    Integer selectGoodsCount(@Param("key") String key);

    List<Map<String, Object>> selectGoods();

    /**
    * @author: Mr.Sun
    * @description: 库存报警
    * @param
    * @return org.jeecg.modules.jxc.homepage.entity.JxcGoods[]
    * @exception
    */
    @MapKey("goods_name")
    Map<String, JxcGoods> selectStockLimit();

}
